//
//  BVMineViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/5/31.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVMineViewController.h"
#import "BVMyPhotoAlbumViewController.h"
#import "BVMyVideoViewController.h"
#import "BVMyCollectionViewController.h"
#import "BVFeedBackViewController.h"
#import "BVClearCacheManager.h"
#import "BVResetPwdViewController.h"
#import "BVLoginViewController.h"
#import "UIView+TransitionAnimation.h"
#import "BVAboutViewController.h"
#import "BVUpgradeViewController.h"

#import "BVMineCell1.h"
#import "BVMineCell2.h"

#import "BVMineDataaViewController.h"
#import "BVLoginAndRegisNav.h"
#import <StoreKit/StoreKit.h>

#define BUY_MINE_TABLEVIEWCELL1 @"BVMineCell1"
#define BUY_MINE_TABLEVIEWCELL2 @"BVMineCell2"

@interface BVMineViewController ()<UITableViewDelegate ,UITableViewDataSource,UINavigationControllerDelegate>

@property (nonatomic ,strong) NSArray          *titleArr;
@property (nonatomic ,strong) UITableView      *tabView;
@property (nonatomic ,strong) UILabel          *nickNameLb;
@property (nonatomic ,strong) UIView           *navView;
@property (nonatomic ,strong) NSArray          *classArr;

@end

@implementation BVMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    [self.navigationItem setTitle:@"个人中心"];
    
    [self layoutUI];
}

-(void)layoutUI{
    
    [self.view addSubview:self.tabView];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *arr = self.titleArr[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 3) {
        
        BVMineCell2 *cell = [tableView dequeueReusableCellWithIdentifier:BUY_MINE_TABLEVIEWCELL2];
        cell.titleLb.text = [NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"titel"]];
        [cell.img setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"img"]]]];
        return cell;
            
    }else{
        BVMineCell1 *cell = [tableView dequeueReusableCellWithIdentifier:BUY_MINE_TABLEVIEWCELL1];
        cell.titleLb.text = [NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"titel"]];
        [cell.img setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"img"]]]];
        
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section == 3) {
        if (indexPath.row == 1) {
            BVClearCacheManager *ClearCache = [[BVClearCacheManager alloc]init];
            [self.hud show:YES];
            [ClearCache notCache:^(NSString *str) {
                [self.hud hide:YES];
                [self showWarning:str];
            } complete:^(NSString *str) {
                [self.hud hide:YES];
                [self showSuccess:str];
            }];
            return;
        }
    }else if (indexPath.section == 4){
        
        BVLoginViewController *loginVC = [[BVLoginViewController alloc]init];
        BVLoginAndRegisNav *nav = [[BVLoginAndRegisNav alloc]initWithRootViewController:loginVC];
        BVKeyWindow.rootViewController = nav;
        return;
    }
    
    NSString *className = [NSString stringWithFormat:@"%@",self.classArr[indexPath.section][indexPath.row]];
    UIViewController *subViewController = [[NSClassFromString(className) alloc] init];
    ROOTNAVPUSH(subViewController, YES);
    
}

-(UIView *)setTableViewHeaderView{

    UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 150)];
    [headerView setBackgroundColor:[UIColor clearColor]];
    [headerView setImage:[UIImage imageNamed:@"information_bac"]];
    [headerView setUserInteractionEnabled:YES];
    
    UITapGestureRecognizer *manTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setMineData)];
    [headerView addGestureRecognizer:manTap];

    UIImageView *avatarImage = [[UIImageView alloc]initWithFrame:CGRectMake(40, 40, 70, 70)];
    [headerView addSubview:avatarImage];
    [avatarImage setImage:[UIImage imageNamed:@"icon-1mindd"]];
    [avatarImage.layer setMasksToBounds:YES];
    [avatarImage.layer setCornerRadius:35.0];

    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [headerView addSubview:settingBtn];
    [settingBtn setBackgroundImage:[UIImage imageNamed:@"shezhi"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(setMineData) forControlEvents:UIControlEventTouchUpInside];
    settingBtn.sd_layout
    .topSpaceToView(headerView, 40)
    .rightSpaceToView(headerView, 15)
    .heightIs(25)
    .widthIs(25);
    
    self.nickNameLb = [[UILabel alloc]init];
    [headerView addSubview:self.nickNameLb];
    self.nickNameLb.textColor = [UIColor whiteColor];
    self.nickNameLb.textAlignment = NSTextAlignmentLeft;
    self.nickNameLb.font = [UIFont systemFontOfSize:14];
    [self.nickNameLb setText:@"网小萌"];
    self.nickNameLb.sd_layout
    .topSpaceToView(headerView, 50)
    .leftSpaceToView(avatarImage,20)
    .heightIs(25)
    .widthIs(100);
    
    return headerView;
}

-(void)setMineData{
    
    BVMineDataaViewController *mineData = [[BVMineDataaViewController alloc]init];
    ROOTNAVPUSH(mineData, YES);
    
}

-(UITableView *)tabView{
    if (!_tabView) {
        self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStylePlain];
        [self.tabView setDelegate:self];
        [self.tabView setDataSource:self];
        [self.tabView setBackgroundColor:[UIColor clearColor]];
        [self.tabView setTableFooterView:[UIView new]];
        [self.tabView setTableHeaderView:[self setTableViewHeaderView]];
        [self.tabView setShowsVerticalScrollIndicator:NO];
        [self.tabView setRowHeight:50];
        [self.tabView registerClass:[BVMineCell1 class] forCellReuseIdentifier:BUY_MINE_TABLEVIEWCELL1];
        [self.tabView registerClass:[BVMineCell2 class] forCellReuseIdentifier:BUY_MINE_TABLEVIEWCELL2];
    }
    return _tabView;
}

-(NSArray *)titleArr{
    if (!_titleArr) {
        self.titleArr = [NSArray arrayWithObjects:
                             @[@{@"img":@"xaingce",@"titel":@"我的相册"},
                               @{@"img":@"shipin",@"titel":@"我的小视频"},
                               @{@"img":@"xaingce",@"titel":@"我的收藏"},
                               @{@"img":@"xaingce",@"titel":@"诚信指数"}],
                             @[@{@"img":@"VIP",@"titel":@"升级VIP"},
                               @{@"img":@"xaingce",@"titel":@"邀请好友"}],
                             @[@{@"img":@"xiugaia",@"titel":@"修改密码"},
                               @{@"img":@"xaingce",@"titel":@"在线客服"},
                               @{@"img":@"xaingce",@"titel":@"意见反馈"}],
                             @[@{@"img":@"guangyu",@"titel":@"关于我们"},
                               @{@"img":@"qinuanun",@"titel":@"清除缓存"}],
                             @[@{@"img":@"tuichu",@"titel":@"退出登录"}],nil];
    }
    return _titleArr;
}

-(NSArray *)classArr{
    if (!_classArr) {
        _classArr = [NSArray arrayWithObjects:
                     @[@"BVMyPhotoAlbumViewController",
                       @"BVMyVideoViewController",
                       @"BVMyCollectionViewController",
                       @"BVMyCollectionViewController"],
                     @[@"BVUpgradeViewController",
                       @"BVMyCollectionViewController",],
                     @[@"BVResetPwdViewController",
                       @"BVResetPwdViewController",
                       @"BVFeedBackViewController"],
                     @[@"BVAboutViewController",
                       @"BVClearCacheManager"],
                     @[@"BVLoginAndRegisNav.h"],nil];
    }
    return _classArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
